<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\PageContentPosition;
use support\Request;
use support\Response;
use plugin\admin\app\model\PageContent;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 首页3d设计 
 */
class PageContentController extends Crud
{
    
    /**
     * @var PageContent
     */
    protected $model = null;

    const default_fields_name = [
        'img' => '图片',
        'img1' => '图片',
        'video' => '视频',
        'active_img' => '动态被选中图片',
        'title' => '标题',
        'desc' => '描述',
        'en_title' => '英文标题',
        'en_desc' => '英文描述',
        'icon' => '图标',
        'active_icon' => '动态被选中图标',
        'sort' => '排序',
        'img_url' => '预览图地址',
        'name' => '姓名',
        'en_name' => '英文姓名',
        'content' => '内容',
        'en_content' => '英文内容'
    ];

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new PageContent;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(Request $request): Response
    {
        $position = PageContentPosition::where('id', $request->get('position_id'))->first()->toArray();

        return view('page-content/index', [
            'position_id' => $request->get('position_id'),
            'hidden_fields' => explode(',', $position['hidden_fields']),
            'operate' => explode(',', $position['operate']),
            'fields_name' => array_merge(self::default_fields_name, json_decode($position['alias'], true) ?? []),
        ]);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }

        $position = PageContentPosition::where('id', $request->get('position_id'))->first()->toArray();

        return view('page-content/insert', [
            'position_id' => $request->get('position_id'),
            'hidden_fields' => explode(',', $position['hidden_fields']),
            'fields_name' => array_merge(self::default_fields_name, json_decode($position['alias'], true) ?? []),
        ]);
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }

        $pageContent = PageContent::where('id', $request->get('id'))->first()->toArray();
        $position = PageContentPosition::where('id', $pageContent['position_id'])->first()->toArray();

        return view('page-content/update', [
            'id' => $request->get('id'),
            'hidden_fields' => explode(',', $position['hidden_fields']),
            'fields_name' => array_merge(self::default_fields_name, json_decode($position['alias'], true) ?? []),
        ]);
    }

}
